package com.cxhc.system.service.app.impl;

import java.util.List;
import java.util.Map;

import com.cxhc.common.utils.DateUtils;
import com.cxhc.system.domain.app.CmApartmentInfo;
import com.cxhc.system.mapper.app.CmApartmentInfoMapper;
import com.cxhc.system.mapper.app.CmHandlingInfoMapper;
import com.cxhc.system.mapper.app.CmHouseholdInfoMapper;
import com.cxhc.system.mapper.app.CmRoomInfoMapper;
import com.cxhc.system.mapper.app.RpCallPoliceMapper;
import com.cxhc.system.service.app.ICmApartmentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 公寓信息Service业务层处理
 * 
 * @author Dhq
 * @date 2024-11-14
 */
@Service
public class CmApartmentInfoServiceImpl implements ICmApartmentInfoService
{
    @Autowired
    private CmApartmentInfoMapper cmApartmentInfoMapper;

    @Autowired
    private CmRoomInfoMapper cmRoomInfoMapper;

    @Autowired
    private CmHouseholdInfoMapper cmHouseholdInfoMapper;

    @Autowired
    private CmHandlingInfoMapper cmHandlingInfoMapper;

    @Autowired
    private RpCallPoliceMapper rpCallPoliceMapper;

    /**
     * 查询公寓信息
     * 
     * @param id 公寓信息主键
     * @return 公寓信息
     */
    @Override
    public CmApartmentInfo selectCmApartmentInfoById(Long id)
    {
        return cmApartmentInfoMapper.selectCmApartmentInfoById(id);
    }

    /**
     * 查询公寓信息列表
     * 
     * @param cmApartmentInfo 公寓信息
     * @return 公寓信息
     */
    @Override
    public List<CmApartmentInfo> selectCmApartmentInfoList(CmApartmentInfo cmApartmentInfo)
    {
        List<CmApartmentInfo> cmApartmentInfos = cmApartmentInfoMapper.selectCmApartmentInfoList(cmApartmentInfo);
        //获取公寓房间数(营业场所)
        Map<String, Map<String, Object>> roomsMap = cmRoomInfoMapper.selectRoomNumByApartId(null);

        Map<String, Map<String, Object>> operateRoomsMap = cmRoomInfoMapper.selectRoomNumByApartId(1);
        //获取公寓常驻人口数
        Map<String, Map<String, Object>> householdsMap = cmHouseholdInfoMapper.selectHouseholdNumByApartId();
        //获取公寓重点人口数
        Map<String, Map<String, Object>> emphasisHouseholdsMap = cmHouseholdInfoMapper.selectEmphasisHouseholdNumByApartId();
        //获取公寓车辆数
        Map<String, Map<String, Object>> carsMap = cmHouseholdInfoMapper.selectCarNumByApartId();
        //获取公寓警情数
        Map<String, Map<String, Object>> handingsMap = cmHandlingInfoMapper.selectHandingNumByApartId();
        //获取微报警数量
        Map<String, Map<String, Object>> callPolicesMap = rpCallPoliceMapper.selectCallPoliceNumByApartId("公寓");

        for (CmApartmentInfo apartmentInfo : cmApartmentInfos) {
            String apartId = String.valueOf(apartmentInfo.getId());
            Map<String, Object> roomObjectMap = roomsMap.get(apartId);
            if (!CollectionUtils.isEmpty(roomObjectMap)){
                apartmentInfo.setRooms(String.valueOf(roomObjectMap.get("nums")));

            }
            Map<String, Object> operateObjectMap = operateRoomsMap.get(apartId);
            if (!CollectionUtils.isEmpty(operateObjectMap)){
                apartmentInfo.setBusiness(String.valueOf(operateObjectMap.get("nums")));

            }
            Map<String, Object> householdsObjectMap = householdsMap.get(apartId);
            if (!CollectionUtils.isEmpty(householdsObjectMap)){
                apartmentInfo.setPopulations(String.valueOf(householdsObjectMap.get("nums")));

            }

            Map<String, Object> emphasisHouseholdsObjectMap = emphasisHouseholdsMap.get(apartId);
            if (!CollectionUtils.isEmpty(emphasisHouseholdsObjectMap)){
                apartmentInfo.setEmphasisPopulations(String.valueOf(emphasisHouseholdsObjectMap.get("nums")));

            }
            Map<String, Object> carsObjectMap = carsMap.get(apartId);
            if (!CollectionUtils.isEmpty(carsObjectMap)){
                apartmentInfo.setCars(String.valueOf(carsObjectMap.get("nums")));

            }
            Map<String, Object> handingsObjectMap = handingsMap.get(apartId);
            if (!CollectionUtils.isEmpty(handingsObjectMap)){
                apartmentInfo.setHandings(String.valueOf(handingsObjectMap.get("nums")));

            }
            long callPoliceNums = 0L;
            for (String addr : callPolicesMap.keySet()) {
                if (addr.contains(apartmentInfo.getApartName())){
                    Map<String, Object> callPoliceObjectMap = callPolicesMap.get(addr);
                    if (!CollectionUtils.isEmpty(callPoliceObjectMap)){
                        callPoliceNums += (Long) callPoliceObjectMap.get("nums");
                    }
                }
            }
            apartmentInfo.setCallPolices(String.valueOf(callPoliceNums));
        }
        return cmApartmentInfos;
    }

    /**
     * 新增公寓信息
     * 
     * @param cmApartmentInfo 公寓信息
     * @return 结果
     */
    @Override
    public int insertCmApartmentInfo(CmApartmentInfo cmApartmentInfo)
    {
        cmApartmentInfo.setCreateTime(DateUtils.getTime());
        return cmApartmentInfoMapper.insertCmApartmentInfo(cmApartmentInfo);
    }

    /**
     * 修改公寓信息
     * 
     * @param cmApartmentInfo 公寓信息
     * @return 结果
     */
    @Override
    public int updateCmApartmentInfo(CmApartmentInfo cmApartmentInfo)
    {
        cmApartmentInfo.setUpdateTime(DateUtils.getTime());
        return cmApartmentInfoMapper.updateCmApartmentInfo(cmApartmentInfo);
    }

    /**
     * 批量删除公寓信息
     * 
     * @param ids 需要删除的公寓信息主键
     * @return 结果
     */
    @Override
    public int deleteCmApartmentInfoByIds(Long[] ids)
    {
        return cmApartmentInfoMapper.deleteCmApartmentInfoByIds(ids);
    }

    /**
     * 删除公寓信息信息
     * 
     * @param id 公寓信息主键
     * @return 结果
     */
    @Override
    public int deleteCmApartmentInfoById(Long id)
    {
        return cmApartmentInfoMapper.deleteCmApartmentInfoById(id);
    }
}
